<?php

class UserController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function getIndex()
	{
		//
            return View::make('backend.users.list', array(
                'title' => 'Liste des utilisateurs',
                'users' => User::all()
            ));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function getCreate()
	{
		//
            return View::make('backend.users.form-create', array(
                'title' => 'Ajouter un utilisateur',
            ));
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function postStore()
	{
		//
            $validator = User::validateNew(Input::all());
            
            if ($validator->fails())
            {
                return Redirect::to('admin/users/create')->withErrors($validator)->withInput(Input::except('password'));;
            }
            else {
                $user = new User;
                $user->name = Input::get('name');
                $user->login = Input::get('login');
                $user->email = Input::get('email');
                $user->is_admin = Input::has('is_admin') ? true : false;
                $user->password = Hash::make(Input::get('password'));
                
                $user->save();
                return Redirect::to('admin/users')->with('status', 'Utilisateur enregistré');
            }
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function getShow($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function getEdit($id)
	{
		//
            return View::make('backend.users.form-edit', array(
                'title' => 'Modifier un utilisateur',
                'user'  => User::find($id),
            ));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function postUpdate($id)
	{
		//
            $user = User::find($id);
            $validator = $user->validate(Input::all());
            
            if ($validator->fails())
            {
                return Redirect::to('admin/users/edit', array($id))->withErrors($validator)->withInput(Input::except('password'));;
            }
            else {
                $user->name = Input::get('name');
                $user->login = Input::get('login');
                $user->email = Input::get('email');
                $user->is_admin = Input::has('is_admin') ? true : false;
                if(Input::has('password')) {
                    $user->password = Hash::make(Input::get('password'));
                }
                $user->save();
                return Redirect::to('admin/users')->with('status', 'Modifications enregistrées');
            }
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function getDelete($id)
	{
		//
            if($id != 1) {
                User::destroy($id);
                return Redirect::to('admin/users')->with('status', 'Utilisateur supprimé');
            }
            return Redirect::to('admin/users')->withErrors(array('Impossible de supprimer cet utilisateur'));
	}
        
        public function getLogin() {
            return View::make('frontend.login', array(
                'title' => 'Connexion',
            ));
        }
        
        public function postLogin() {
                if (Auth::attempt(array('login' => Input::get('login'), 'password' => Input::get('password'))))
                {
                        return Redirect::to('admin/dashboard');
                }
                else {
                        return Redirect::to('/');
                }
        }
        
        public function getLogout() {
                if(Auth::check()) {
                        Auth::logout();
                }
                return Redirect::to('/');
        }
}
